package com.sample.springbatch.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zwg on 2016/10/12.
 */
public class DateUtil {

    public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_NO_SPLIT_CHAR = "yyyyMMddHHmmss";
    public static final String PATTERN_NO_SECOND = "yyyyMMddHHmm";
    public static final String PATTERN_DATE = "yyyy-MM-dd";
    public static final String PATTERN_DATE_NO_SPLIT_CHAR = "yyyyMMdd";


    /**
     * 按照pattern所指示的样式格式化日期
     *
     * @param date
     * @param pattern
     * @return String
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }


    /**
     * 格式化日期默认格式(yyyyMMddHHmm)
     * @param date
     * @return
     */
    public static String formatNoSecond(Date date) {
        return format(date,PATTERN_NO_SECOND);
    }

    /**
     * 格式化日期默认格式(yyyy-MM-dd HH:mm:ss)
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date,PATTERN_DEFAULT);
    }


    /**
     * 按照pattern格式转成日期
     * @param date
     * @param pattern
     * @return Date
     */
    public static Date parse(String date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 按照默认格式(yyyyMMddHHmm)转成日期
     * @param date
     * @return
     */
    public static Date parseNoSecond(String date) {
        return parse(date,PATTERN_NO_SECOND);
    }

    /**
     * 按照默认格式(yyyyMMddHHmm)转成日期
     * @param date
     * @return
     */
    public static Date parseNoSplit(String date) {
        return parse(date,PATTERN_NO_SPLIT_CHAR);
    }

    /**
     * 按照默认格式(yyyy-MM-dd HH:mm:ss)转成日期
     * @param date
     * @return
     */
    public static Date parse(String date) {
        return parse(date,PATTERN_DEFAULT);
    }

    /**
     * 获取指定日期
     * @param date
     * @param day
     * @return
     */
    public static Date getDate(Date date,int day){
        Calendar calendar = Calendar.getInstance();
        if(null!=date){
            calendar.setTime(date);
        }
        calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR)+day);
        return calendar.getTime();
    }

    /**
     * 获取当前是周几
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date){
        Calendar cal = Calendar.getInstance();
        if(date!=null){
            cal.setTime(date);
        }

        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取给定时间对应的当天毫秒数
     * @param time "HH:mm:ss"
     * @return
     */
    public static long getTimeMillis(String time){
        return getTimeMillis(time,new Date());
    }

    /**
     * 获取给定时间对应的当天毫秒数
     * @param time "HH:mm:ss"
     * @param date 为空 默认当天
     * @return
     */
    public static long getTimeMillis(String time,Date date) {
        if(date==null){
            date=new Date();
        }
        Date currentDate = parse(format(date,PATTERN_DATE)+ " " +time);
        return currentDate.getTime();
    }

}
